#include "TcpIpCom.h"

TcpIpCom::TcpIpCom(QObject *parent)
	: QObject(parent)
	, sign(0)
{
}

TcpIpCom::~TcpIpCom()
{
}

bool TcpIpCom::createTcpIpCom(bool isServer, int comNumber, const char* ipAddress, int timeout)
{
	bool b;
	b = tcpIP.CreateConnection(isServer, comNumber, ipAddress, timeout);
	return b;
}

void TcpIpCom::receiveSign(int i)
{
	sign = i;
}

bool TcpIpCom::isConnected()
{
	bool b = true;
	sign = 0; 
	while (1) {
		b = tcpIP.IsConnected();
		if (b)
			break; 
		if (sign == 1)
			break;
	}
	emit sendComSign(b);
	return b;
}

bool TcpIpCom::disconnect()
{
	bool b;
	b = tcpIP.Disconnect();
	return b;
}

int TcpIpCom::sendData(const char* data, int len)
{
	int ret = 0;
	ret = tcpIP.SendData(data, len);
	return ret;
}

int TcpIpCom::getData()
{
	sign = 0;
	int ret = 0;
	char data;
	int len = 10;
	while (1) {
		ret = tcpIP.GetData(&data, len);
		if (ret > 0) {
			emit getComData(&data);
			break;
		}
		if (sign == 1)
			break;
	}  
	return ret;
}


